/*
 * XXX
 */

package edu.uhcl.csci4838.group3.pendingIntents;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

/**
 * XXX
 * 
 * @author Tyler Hays
 */
public abstract class WakeUpReminderIntentService extends IntentService {
	
	/**
	 * used for debugging
	 */
	public static final String LOCK_NAME_STATIC = "edu.uhcl.csci4838.group3.notification";
	private static PowerManager.WakeLock lockStatic = null;
	
	/**
	 * Makes sure the devices is in needed stated
	 * 
	 * @param context
	 *            context being used
	 */
	public static void acuireStaticLock(final Context context) {
		WakeUpReminderIntentService.getLock(context)
				.acquire();
	}
	
	synchronized private static PowerManager.WakeLock getLock(final Context context) {
		if (WakeUpReminderIntentService.lockStatic == null) {
			final PowerManager mgr = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
			WakeUpReminderIntentService.lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
					WakeUpReminderIntentService.LOCK_NAME_STATIC);
			WakeUpReminderIntentService.lockStatic.setReferenceCounted(true);
		}
		return (WakeUpReminderIntentService.lockStatic);
	}
	
	/**
	 * XXX
	 * 
	 * @param name
	 */
	public WakeUpReminderIntentService(final String name) {
		super(name);
	}
	
	/**
	 * XXX
	 * 
	 * @param intent
	 *            intent to be used
	 */
	public abstract void doReminderWork(Intent intent);
	
	/**
	 * {@inheritDoc}
	 * 
	 * @param intent
	 *            {@inheritDoc}
	 */
	/* 
	 * (non-Javadoc)
	 * 
	 * @see android.app.IntentService#onHandleIntent(android.content.Intent)
	 */
	@Override
	final protected void onHandleIntent(final Intent intent) {
		try {
			this.doReminderWork(intent);
		} finally {
			WakeUpReminderIntentService.getLock(this)
					.release();
		}
		
	}
}
